<template>
  <a-layout id="components-layout-demo-top" class="layout" style="min-height: 100%;width:100%">
    <div id="header">
      <div class="logo">知&nbsp;否</div>

      <div id="title">

        <router-link :to="{name:'mainPage',params:{'id':this.id,'img':this.img,'token':this.token}}" style="color:white">
          <a-icon type="home" />&nbsp;首页
        </router-link>
      </div>

      <div id="title1">
        <router-link :to="{name:'personSpace',params:{'id':this.id,'img':this.img,'token':this.token}}" style="color:white">
          <a-icon type="user" />&nbsp;个人中心
        </router-link>
      </div>



      <a-input-search placeholder="请输入要搜索的问题" v-model="searchItem" style="width: 300px;margin: 15px 20px 10px 90px;" @search="onSearch"/>
      <button type="button" id="search"  style="
    height: 15px;
    width: 120px;
    margin: 10px;
    padding: 18px;
    border-radius: 10px;
    line-height: 0px;
    font-family: 'Source Sans Pro', sans-serif;
    font-size: 20px;
    position: absolute;
    background: deepskyblue;
    font-weight: bold;
    color: white;
    border-color: skyblue;
    /*transform: translateY(4px);*/
">
        <router-link :to="{name:'addQuestion',params:{'id':this.id,'img':this.img,'token':this.token}}" style="color:white">
          提问题
        </router-link>

      </button>

      <div style="height:60px;weight:200px;float:right;padding: 0 20px;">


        <div id="right" style="padding: 15px 5px 20px 20px;">
          <a-badge :count=count title="通知" v-if=ifToken style="margin-left: 40px">
            <a-icon type="bell" style="font-size: 25px" title="通知" @click="getMessage"/>
          </a-badge>

          <!--          头像<router-link :to="{name:'personSpace',params:{'id':this.id,'img':this.img,'token':this.token}}">
                      <a-avatar shape="square" v-if=ifToken size="large" :src=img style="margin-left: 20px;margin-bottom: 12px"/>
                    </router-link>-->
        </div>

        <template>
          <a-dropdown>
            <a style="color:white;font-size: 20px;" >
              {{ u_name }} <a-icon type="down" style="color:white;font-size:25px" />
            </a>
            <a-menu slot="overlay" @click="onClick">
              <a-menu-item key="1">
                <a-icon type="setting" />
                设置
              </a-menu-item>
              <a-menu-item key="2" @click="logout">
                <a-icon type="logout" />
                退出
              </a-menu-item>

            </a-menu>
          </a-dropdown>
        </template>

      </div>
    </div>




    <a-layout-content style="padding: 0 50px">
      <a-breadcrumb style="margin: 20px 0">
      </a-breadcrumb>
      <div :style="{ background: '#fff', padding: '24px', minHeight: '430px' }">
<!--        内容区域-->
        <!--依次类推的话，以传来的列表list中的msg_id为key，依次显示各个消息，具体如下-->
        <div v-for="item in list" :key="item.msg_id">
          {{item.msg_content}}
          <p style="margin-left: 700px;display: inline">{{item.msg_time}}</p>
          <a-button style="width:80px;padding: 0 10px;" type="danger" @click="deleteMsg(item)">删除消息</a-button>
          <a-divider/>
        </div>
        <!--end 显示问题列表的部分-->
      </div>
    </a-layout-content>

  </a-layout>
</template>
<script>
export default {
  inject:['reload'],
  name:"messagePage",
  data(){
    return{
      list: [],
      count:'',
      user_id:this.$route.params.id,
      id:this.$route.params.id,//从登陆界面进行传值
      img:this.$route.params.img,
      token:this.$route.params.token,
      searchItem:'',
      u_name:'',
      ifToken: localStorage.getItem('access_token'),
    }
  },
  mounted(){
    var _this = this;
    _this.$axios.post('/api/user/selectByUserId',
        _this.$qs.stringify({
          uId:_this.id,
        }),
        {headers:{access_token:_this.token}}
    ).then(function (response) {
      console.log("主页获取登录用户信息：");
      console.log(response.data);
      console.log(response.data.data.u_img);
      console.log(response.data.data.u_name);
      _this.img = response.data.data.u_img;
      _this.u_name=response.data.data.u_name;
    }).catch(function (error) {
      console.log(error);
      //_this.$message.error('请求失败');
    })


    _this.$axios.post('/api/msg/showMsg',
      _this.$qs.stringify({
        userId: _this.$route.params.id,//router传值！！！
      }),{headers:{access_token:_this.token}}
    ).then(function (response) {
      console.log(response.data);
      _this.list = response.data.data;
      if(response.data.code == -1)
        _this.$message.success('没有消息');
    }).catch(function (error) {
      console.log(error);
      _this.$message.error('获取消息失败');
    })
  },
  methods:{
    logout(){
      var _this = this;
      _this.$axios.post('/api/user/leave',
          _this.$qs.stringify({
            uId: this.id,
          }),{headers: {access_token:_this.token}}
      ).then(function (response) {
        console.log(response.data);
        localStorage.removeItem('access_token');
        localStorage.clear();
        _this.$router.push({
          path:'/login',
          name:'login',
        })
      }).catch(function (error) {
        console.log(error);
        _this.$message.error('请求失败');
      })
    },//end exit函数/*
    onClick ({ key }) {
      console.log(`Click on item ${key}`)
    },
    onSearch(){
      var _this = this;
      _this.$axios.post('/api/quest/selectByTitle',
          _this.$qs.stringify({
            questionTitle:_this.searchItem,
          }),{ headers: { access_token: _this.token } }
      ).then(function (response) {
        console.log(response.data.data);
        _this.$router.push({
          path:'/searchPage',
          name:'searchPage',//路由命名
          params:{
            list:response.data.data,
            id:_this.id,
            img:_this.img,
            token:_this.token
          },
        });//end 页面跳转
      }).catch(function (error) {
        console.log(error);
        _this.$message.error('请求失败');
      })
    },//end onSearch函数，进行查找问题

    getMessage(){
      var _this = this;
      _this.$router.push({
        path:'/messagePage',
        name:'messagePage',
        params:{
          id:_this.id,
          img:_this.img,
          token:_this.token,
        }
      })
    },//end getMessage函数，获取消息

    messageNum(){
      var _this = this;
      _this.$axios.post('/api/msg/showMsgNum',
          _this.$qs.stringify({
            userId: _this.$route.params.id,//router传值！！！
          }),{headers:{access_token:_this.token}}
      ).then(function (response) {
        console.log(response.data.data);
        _this.count = response.data.data;
        //_this.list = response.data.data;
      }).catch(function (error) {
        console.log(error);
        _this.$message.error('获取消息失败');
      })
    },

    deleteMsg(record){
      var _this = this;
      _this.$axios.post('/api/msg/deleteMsg',
          _this.$qs.stringify({
            msgId:record.msg_id,
            isDeleted:1,
          }),{headers:{access_token:_this.token}}
      ).then(function (response) {
        console.log(response);
        //_this.count = response.data.data;
        //_this.list = response.data.data;
      }).catch(function (error) {
        console.log(error);
        _this.$message.error('获取消息失败');
      })
      _this.reload()
      // _this.$axios.post('/api/msg/showMsg',
      //     _this.$qs.stringify({
      //       userId: _this.$route.params.id,//router传值！！！
      //     }),{headers:{access_token:_this.token}}
      // ).then(function (response) {
      //   console.log(response.data);
      //   _this.list = response.data.data;
      //   if(response.data.code == -1)
      //     _this.$message.success('没有消息');
      // }).catch(function (error) {
      //   console.log(error);
      //   _this.$message.error('获取消息失败');
      // })
    },
  }
}
</script>
<style>

</style>
